package com.harmontronics.hanma.paas.paas.controller;

import com.harmontronics.hanma.paas.paas.domain.organization.entity.UserGroupTree;
import com.harmontronics.hanma.paas.paas.dto.iam.output.HmUserGroupTreeResult;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import cn.hutool.core.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmUserGroupAppService;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmUserGroupCreateInput;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmUserGroupUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmUserGroup;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 用户组
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-07 10:35:20
 */
@RestController
@RequestMapping("/api/hmusergroup")
@Api(tags = {"用户组"})
public class HmUserGroupController {
    @Autowired
    private HmUserGroupAppService hmUserGroupappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("获取应用的用户组列表树")
    public ResponseEntity<List<HmUserGroupTreeResult>> list() {
        List<UserGroupTree> hmUserGroupList = hmUserGroupappService.selectUserGroupTree();

        return ResponseEntity.ok(Convert.toList(HmUserGroupTreeResult.class, hmUserGroupList));
    }

    @GetMapping("/listByAppUserId")
    @ApiOperation(value = "获取用户角色组列表树")
    public ResponseEntity<List<HmUserGroupTreeResult>> listByAppUserId(
            @RequestParam Long userId) {
        List<UserGroupTree> hmUserGroupList = hmUserGroupappService.selectUserGroupTreeByAppUserId(userId);
        return ResponseEntity.ok(Convert.toList(HmUserGroupTreeResult.class, hmUserGroupList));
    }


    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmUserGroupCreateInput hmUserGroupInput,BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmUserGroup hmUserGroup =BeanUtil.toBean(hmUserGroupInput,HmUserGroup.class);
            hmUserGroupappService.save(hmUserGroup);

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmUserGroupUpdateInput hmUserGroupInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmUserGroup hmUserGroup =BeanUtil.toBean(hmUserGroupInput,HmUserGroup.class);
            hmUserGroupappService.update(hmUserGroup);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
            hmUserGroupappService.remove(Arrays.asList(ids));
        return ResponseEntity.ok();
    }

    @PutMapping("/up")
    @ApiOperation(value = "上移用户组节点")
    public ResponseEntity moveUp(@RequestParam Long id) {
        hmUserGroupappService.moveUp(id);
        return ResponseEntity.ok();
    }

    @PutMapping("/down")
    @ApiOperation(value = "下移用户组节点")
    public ResponseEntity moveDown( @RequestParam Long id) {
        hmUserGroupappService.moveDown(id);
        return ResponseEntity.ok();
    }

    @GetMapping("/validate-field")
    @ApiOperation(value = "表单字段验证")
    public ResponseEntity validateField(@RequestParam Long appId, @RequestParam long id, @RequestParam String key,
            @RequestParam String value, @RequestParam long parentId) {
        hmUserGroupappService.validateField(appId, id, key, value, parentId);
        return ResponseEntity.ok();
    }

}
